#include "qprojectplugin.h"

#include "projectwidget.h"

#include "../../include/qfindresult.h"
#include "../../lib/shared/qproject.h"

#include <qplugin.h>
#include <QVBoxLayout>


QProjectWidget::QProjectWidget(AbstractCore *core):
    m_core(core)
{
    m_widget=new ProjectWidget(core);
    m_text=tr("Project");
    m_icon=QIcon(":/images/project.png");
}

QString QProjectWidget::text()
{
    return m_text;
}

QIcon   QProjectWidget::icon()
{
    return m_icon;
}

int     QProjectWidget::index()
{
    return 0;
}

QWidget* QProjectWidget::widget()
{
    return m_widget;
}

bool    QProjectWidget::enabled()
{
    return true;
}

QFindResult* QProjectWidget::search(const QString& text,bool sensitive,bool whole)
{
    QFindResult *result=new QFindResult;
    result->m_page=m_text;
    QProject *p=QProject::project();
    FindResult *r;
    r=check("Project Name",p->getProjectName(),text,sensitive,whole);
    if(r!=NULL)
    {
        result->m_results.append(r);
    }

    r=check("Project Path",p->getProjectPath(),text,sensitive,whole);
    if(r!=NULL)
    {
        result->m_results.append(r);
    }

    QString str;
    int w,h;
    w=p->getDesignSize().width();
    h=p->getDesignSize().height();
    if(QString::number(w)==text || QString::number(h)==text)
    {
        str.sprintf("%d x %d",p->getDesignSize().width(),p->getDesignSize().height());
        r=check("Designer Size",str,text,sensitive,whole);
        if(r!=NULL)
        {
            result->m_results.append(r);
        }
    }

    w=p->getRunningSize().width();
    h=p->getRunningSize().height();
    if(QString::number(w)==text || QString::number(h)==text)
    {
        str.sprintf("%d x %d",p->getDesignSize().width(),p->getDesignSize().height());
        r=check("Running Size",str,text,sensitive,whole);
        if(r!=NULL)
        {
            result->m_results.append(r);
        }
    }

    return result;
}

FindResult* QProjectWidget::check(const QString &name,const QString &value, const QString &text, bool sensitive, bool whole)
{
    FindResult* r=NULL;
    QString str1,str2;
    if(sensitive)
    {
        str1=value.toLower();
        str2=text.toLower();
    }
    if(str1.indexOf(str2)>=0)
    {
        r=new FindResult;
        r->insert("name",name);
        r->insert("value",value);
    }
    return r;
}

void QProjectPlugin::initPlugin(AbstractCore *core)
{
    QProjectWidget *wid;
    wid=new QProjectWidget(core);
    emit addObject(wid);
}

Q_EXPORT_PLUGIN2(QProjectPlugin, QProjectPlugin)
